// pages/process/index.js

var api = require('../../config/api.js');
var util = require('../../utils/util.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    companyName: '',
    navHeight: app.globalData.appInfo.navHeight,
    active: 1,
    steps: [{
        text: '基本信息',
        desc: '描述信息'
      },
      {
        text: '创新指标',
        desc: '描述信息'
      },
      {
        text: '财务数据',
        desc: '描述信息'
      }
    ],
    form: {
      isAccident: '2',
      estiblishDate: null,
      industry: null,
      name: null,
      regLocation: null
    },
    creativeForm: {
      authFmzl: 0,
      applyFmzl: 0,
      authJcdl: 0,
      applyJcdl: 0,
      authSyxx: 0,
      applySyxx: 0,
      authWgsj: 0,
      applyWgsj: 0,
      authZzq: 0,
      applyZzq: 0,
      buyZscq: 0,
      isCybzbz: '2',
      isHzgx: '1',
    },
    kjcgzh: '预计25分左右',
    yjkfzz: '预计18分左右',
    show: false,
    financeForm: {
      beforeLastYearJzc: '',
      lastYearJzc: '',
      nowYearJzc: '',
      beforeLastYearSr: '',
      lastYearSr: '',
      nowYearSr: '',
      beforeLastYearYffy: '',
      lastYearYffy: '',
      nowYearYffy: ''
    },
    growNumber: 0, //企业成长性得分
    actions: [{
        name: '查账征收',
      },
      {
        name: '核定征收',
      }
    ],
    submitPhone: '',
    /** 联系电话 */
    connectPhone: '',
    overlay: false,
    dialogIndex: 0,
    yearAction: {} //财务数据年份数组
  },
  /** 输入通用修改数据方法 */
  onInput (e) {
    const { key } = e.currentTarget.dataset;
    this.setData({
      [key]: e.detail
    });
  },
  
  /** 税务征收方式 选项弹框打开 */
  onHandsheet() {
    this.setData({
      show: true
    });
  },
  /** 税务征收方式 选项弹框关闭 */
  onClose() {
    this.setData({
      show: false
    });
  },
  /** 税务征收方式 选择事件 */
  onSelect(event) {
    this.data.form.swzsfs = event.detail.name
    var tempForm = this.data.form;
    this.setData({
      form: tempForm
    });
    this.onClose()
  },
  /** 高企认定前一年内未发生重大安全、重大质量事故或严重违法行为 */
  isAccidentChange(option){
    this.data.form.isAccident = option.detail
    var tempForm = this.data.form;
    this.setData({
      form: tempForm
    });
  },
  /** 是否参与编制标准 */
  isCybzbzChange(option){
    this.data.creativeForm.isCybzbz = option.detail
    var tempForm = this.data.creativeForm;
    this.setData({
      creativeForm: tempForm
    });
  },
  /** 与高校建立合作关系 */
  isHzgxChange(option){
    this.data.creativeForm.isHzgx = option.detail
    var tempForm = this.data.creativeForm;
    this.setData({
      focreativeFormrm: tempForm
    });
  },
  /** 计算企业成长性得分 */
  changeFinanceInput(e) {
    var value = e.detail.value;
    var that = this;
    var financeForm = this.data.financeForm;
    const { key } = e.currentTarget.dataset;
    this.setData({
      [key]: value
    });
    if (!(/(^[0-9]*$)/.test(value))) {
      util.showErrorToast("输入值需为整数");
    }else if(!util.isNullOrEmpty(financeForm.beforeLastYearJzc) && !util.isNullOrEmpty(financeForm.lastYearJzc) && !util.isNullOrEmpty(financeForm.nowYearJzc) && !util.isNullOrEmpty(financeForm.beforeLastYearSr) && !util.isNullOrEmpty(financeForm.lastYearSr)
    && !util.isNullOrEmpty(financeForm.nowYearSr)){
      wx.request({
        url: api.calculateGrowNumber,
        data: {
          financeForm: financeForm
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: function(res) {
          console.log(res);
          if (res.data.result == 1) {
            that.setData({
              growNumber: res.data.info
            });
          }
        }
      });
    }
  },
  /** 知产数量增加/减少 事件 */
  onStepChange(e) {
    const {
      key
    } = e.currentTarget.dataset;
    console.log(e.detail);
    this.setData({
      [key]: e.detail
    });
  },
  /** 页面最下方问题点击事件 */
  showHelp(e) {
    console.log(e)
    this.setData({
      dialogIndex: e.currentTarget.dataset.dialogIndex,
      overlay: true
    });
  },
  /** 问题咨询输入联系电话号码 */
  changeConnectPhone(e) {
    this.setData({
      connectPhone: e.detail.value
    });
  },
  /** 关闭问题咨询页面 */
  closeDialog() {
    this.setData({
      overlay: false
    });
  },
  inbtn:function(e){     
    //这个事件必须有，就算不做什么事情也要写上去，因为这个事件是为了防止事件冒泡，导致点击dialog这里面的元素时，弹出框关闭。
    console.log("in")
  },
  /** 问题咨询输入号码后确认事件 */
  okPhoneNum() {
    var connectPhone = this.data.connectPhone;
    if(!/^1(3|4|5|7|8)\d{9}$/.test(connectPhone)){
      util.showErrorToast("手机号码错误");
    }else{
      wx.showToast({
        title: '提交成功',
        icon: 'success'
      });
    }
  },
  /** 上一步 */
  goPrevStep() {
    const {
      active
    } = this.data;
    const prev = active - 1;
    if (prev < 1) return;
    this.setData({
      active: prev
    });
  },
  /** 下一步 */
  goNextStep() {
    const {
      active,
      form
    } = this.data;
    // 第一步的表单验证
    if (active === 1) {
      if (util.isNullOrEmpty(form.estiblishDate)) {
        wx.showToast({
          title: '请输入公司成立时间',
          icon: 'none'
        });
        return;
      }
      if (util.isNullOrEmpty(form.industry)) {
        wx.showToast({
          title: '请输入所属行业',
          icon: 'none'
        });
        return;
      }
      if (util.isNullOrEmpty(form.regLocation)) {
        wx.showToast({
          title: '请输入所属区域',
          icon: 'none'
        });
        return;
      }
      if (util.isNullOrEmpty(form.staffNumRange)) {
        wx.showToast({
          title: '请输入员工人数',
          icon: 'none'
        });
        return;
      }
      if (util.isNullOrEmpty(form.technologyPeopleNum)) {
        wx.showToast({
          title: '请输入科技人员人数',
          icon: 'none'
        });
        return;
      }
      if (util.isNullOrEmpty(form.socialStaffNum)) {
        wx.showToast({
          title: '请输入社保人数',
          icon: 'none'
        });
        return;
      }
      if (util.isNullOrEmpty(form.swzsfs)) {
        wx.showToast({
          title: '请选择税务征收方式',
          icon: 'none'
        });
        return;
      }
    }
    if (active === 2) {
      console.log(this.data.creativeForm);
    }
    const next = active + 1;
    if (next > 3) return;
    this.setData({
      active: next
    });
  },
  /** 提交 */
  submit() {
    var that = this;
    var financeForm = this.data.financeForm;
    if(!util.isNullOrEmpty(financeForm.beforeLastYearJzc) && !util.isNullOrEmpty(financeForm.lastYearJzc) && !util.isNullOrEmpty(financeForm.nowYearJzc) && !util.isNullOrEmpty(financeForm.beforeLastYearSr) && !util.isNullOrEmpty(financeForm.lastYearSr)
    && !util.isNullOrEmpty(financeForm.nowYearSr) && !util.isNullOrEmpty(financeForm.beforeLastYearYffy) && !util.isNullOrEmpty(financeForm.lastYearYffy) && !util.isNullOrEmpty(financeForm.nowYearYffy)){
      that.setData({
        overlay: true,
        dialogIndex: 2
      });
    }else {
      util.showErrorToast("输入值不能为空");
      return;
    }
  },
  /** 查看测试结果输入手机号 */
  changeSubmitPhone(e) {
    this.setData({
      submitPhone: e.detail.value
    });
  },
  /** 查看测试结果 */
  checkTestResult() {
    const {
      submitPhone
    } = this.data;
    if (submitPhone === '') {
      wx.showToast({
        title: '请输您的手机号码',
        icon: 'none'
      });
      return;
    }
    if (!/^1(3|4|5|7|8)\d{9}$/.test(submitPhone)) {
      util.showErrorToast("手机号码错误");
      return;
    }
    var that = this;
    wx.request({
      url: api.submitHighEvaluationForm,
      data: {
        form: that.data.form,
        financeForm: that.data.financeForm,
        creativeForm: that.data.creativeForm,
        growNumber: that.data.growNumber,
        connectPhone: that.data.connectPhone,
        submitPhone: that.data.submitPhone,
        companyName: that.data.companyName,
        userId: wx.getStorageSync('appUserInfo').id
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        console.log(res);
        if (res.data.result == 1) {
          wx.navigateTo({
            url: '../report/index?highEvaluationResult=' + JSON.stringify(res.data.info),
          });
        }
      }
    });
  },
  /** 输入开票产品名称 */
  onChange(e) {
    this.setData({
      ['form.kpcpmc']: e.detail
    }, () => {
      console.log(this.data.form.kpcpmc);
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var date = new Date();
    var nowYear = date .getFullYear();
    var lastYear = nowYear - 1;
    var beforeLastYear = nowYear - 2;
    var phone = wx.getStorageSync('appUserInfo').telPhone;
    this.setData({
      companyName: options.companyName,
      yearAction: {
        nowYear: nowYear,
        lastYear: lastYear,
        beforeLastYear: beforeLastYear
      },
      connectPhone: phone,
      submitPhone: phone
    });
    this.getCompanyByName();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /** 获取企业信息数据 */
  getCompanyByName() {
    var that = this;
    util.request(api.getCompanyByName, {
      companyName: that.data.companyName
    }, 'GET').then(res => {
      if (res.result == 1) {
        that.setData({
          form: res.info
        });
      }
    });
  },
})